/*
1306. 跳跃游戏 III
https://leetcode.cn/problems/jump-game-iii/description/
中等 2024/12/04
*/

class Solution {
public:
    bool canReach(vector<int>& arr, int start) {
        int n=arr.size();
        queue<int>qe;
        vector<bool>visited(n,false);
        qe.push(start);
        while(!qe.empty())
        {
            int k=qe.front();
            qe.pop();
            int a=arr[k]+k;
            int b=k-arr[k];
            if((a<n&&arr[a]==0)||(b>=0&&arr[b]==0)) return true;
            if(a<n&&!visited[a]) 
            {
                qe.push(a);
                visited[a]=true;
            }
            if(b>=0&&!visited[b]) 
            {
                qe.push(b);
                visited[b]=true;
            } 
        }
        return false;
    }
};